//
//  InstallationDetailTabPanelNumberCell.swift
//  ezyform
//
//  Created by MoennigQ on 28/2/19.
//  Copyright © 2019 Client.Solution Pty Ltd. All rights reserved.
//
import Alamofire
import SDWebImage
import SnapKit
class InstallationDetailTabPanelNumberCell: TableViewCell {
    let panelNumberImageView = UIImageView()
    
    let panelNumberLabel: BaseLabel = BaseLabel()
    let panelBrandLabel: BaseLabel = BaseLabel()
    let panelModelLabel: BaseLabel = BaseLabel()
    
    let invalidReasonLabel: BaseLabel = ViewFactory.shared.generateRequiredLabel()
    let localFlagImageView = UIImageView()
    let spvStatusImageView = UIImageView()
    let activityView = UIActivityIndicatorView()
    
    var spvStatusImageViewWidthConstant: Constraint!
    
    override func setUpviews(){
        self.backgroundColor = .clear
        //seq no
        panelNumberImageView.drawCorner(byRoundingCorners: .allCorners,radii: 3)
        panelNumberImageView.contentMode = .scaleAspectFill
        panelNumberImageView.clipsToBounds = true
        self.addSubview(panelNumberImageView)
        panelNumberImageView.snp.makeConstraints { (make) in
            make.top.equalTo(12)
            make.left.equalToSuperview()
            make.width.height.equalTo(64)
            make.bottom.equalTo(-12)
        }
        // 停止后，隐藏菊花
        activityView.hidesWhenStopped = true
        //Style: whiteLarge比较大的白色环形进度条;white白色环形进度条;gray灰色环形进度条
        activityView.style = UIActivityIndicatorView.Style.white
        self.addSubview(activityView)
        activityView.snp.makeConstraints { (make) in
            make.center.equalTo(panelNumberImageView)
        }
        //localFlag
        self.localFlagImageView.image = UIImage(named: "uploadImageFailed")
        self.addSubview(localFlagImageView)
        localFlagImageView.snp.makeConstraints { (make) in
            make.right.bottom.equalTo(panelNumberImageView)
            make.size.equalTo(CGSize(width: 16, height: 11))
        }
        
        //panelNumber
        panelNumberLabel.textColor = UIColor(hexStr: "#292D32")
        panelNumberLabel.setFont(R.font.interMedium(size: 15))
        panelNumberLabel.textAlignment = .left
        panelNumberLabel.numberOfLines = 2
        panelNumberLabel.lineBreakMode = .byCharWrapping
        self.addSubview(panelNumberLabel)
        panelNumberLabel.snp.makeConstraints { (make) in
            make.top.equalTo(panelNumberImageView)
            make.left.equalTo(panelNumberImageView.snp.right).offset(12)
            make.width.width.greaterThanOrEqualTo(0)
        }
        //panel brand
        panelBrandLabel.textColor = UIColor(hexStr: "#292D3299")
        panelBrandLabel.setFont(R.font.interRegular(size: 11))
        panelBrandLabel.textAlignment = .left
//        panelBrandLabel.adjustsFontSizeToFitWidth = true
//        panelBrandLabel.minimumScaleFactor = 0.2
        panelBrandLabel.numberOfLines = 2
        self.addSubview(panelBrandLabel)
        panelBrandLabel.snp.makeConstraints { (make) in
            make.top.equalTo(panelNumberLabel.snp.bottom)
            make.left.equalTo(panelNumberLabel)
            make.right.equalTo(-16)
            make.height.greaterThanOrEqualTo(0)
        }
        //panel model
        panelModelLabel.textColor = UIColor(red: 0.61, green: 0.61, blue: 0.61,alpha:1)
        panelModelLabel.setFont(R.font.sfProTextRegular(size: 12))
        panelModelLabel.textAlignment = .left
        panelModelLabel.adjustsFontSizeToFitWidth = true
        self.addSubview(panelModelLabel)
        panelModelLabel.snp.makeConstraints { (make) in
            make.top.equalTo(panelBrandLabel.snp.bottom)
            make.left.equalTo(panelNumberLabel)
            make.right.equalTo(panelBrandLabel)
            make.height.greaterThanOrEqualTo(0)
            make.bottom.lessThanOrEqualTo(-2)
        }
        
        //spv status
        self.addSubview(spvStatusImageView)
        spvStatusImageView.snp.makeConstraints { (make) in
            make.top.equalTo(panelNumberImageView)
            make.right.equalTo(-16)
            self.spvStatusImageViewWidthConstant = make.width.equalTo(0).constraint
            make.height.equalTo(20)
            make.left.equalTo(panelNumberLabel.snp.right)
        }
        //invalid reason
        invalidReasonLabel.numberOfLines = 3
        invalidReasonLabel.adjustsFontSizeToFitWidth = true
        self.addSubview(invalidReasonLabel)
        invalidReasonLabel.snp.makeConstraints { (make) in
            make.top.equalTo(panelNumberLabel.snp.bottom)
            make.left.equalTo(panelNumberLabel)
            make.right.equalToSuperview().offset(-16)
            make.bottom.lessThanOrEqualTo(-2)
        }
        //line view
        let lineView = UIView()
        lineView.backgroundColor = UIColor(hexStr: Constant.SeparatorColorHexStr)
        self.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.left.equalTo(panelNumberImageView)
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }
    
    func setData(_ panelModel: PanelNumberImageDBModel){
        //符合校验规则时的barcode 的字体颜色
        self.panelBrandLabel.text = ""
        self.panelModelLabel.text = ""
        self.invalidReasonLabel.text = ""
        self.panelNumberLabel.text = panelModel.code
        //不符合panel number的校验规则，code显示为红色
        if !Tool().isValidPanelNumber(str: panelModel.code) {
            self.panelNumberLabel.textColor = .red
        }else{
            self.panelNumberLabel.textColor = UIColor(hexStr: "#292D32")
        }
        //operType为空，表示是server 的数据
        if panelModel.operType == "" {
            //设置spv status,如果当前Job开启了SPV，就显示每个Panel Number的spv 验证状态
            if JobDetailModelHelper.shared.jobDetailModel.spv_check {
                
                switch panelModel.spv_status.lowercased() {
                case PanelNumberSPVStatus.verifying.rawValue,PanelNumberSPVStatus.unverified.rawValue:
                    spvStatusImageViewWidthConstant.update(offset: 72)
                    self.spvStatusImageView.image = UIImage(named: "installation_detail_panelnumber_spv_verifying")
                case PanelNumberSPVStatus.invalid.rawValue:
                    spvStatusImageViewWidthConstant.update(offset: 57)
                    self.spvStatusImageView.image = UIImage(named: "installation_detail_panelnumber_spv_invalid")
                    self.invalidReasonLabel.text = "Invalid Reason: " + (panelModel.spv_error_message) 
                case PanelNumberSPVStatus.verified.rawValue:
                    //verified即时不符合校验规则，也显示为正常颜色
                    spvStatusImageViewWidthConstant.update(offset: 65)
                    panelNumberLabel.textColor = UIColor(hexStr: "#292D32")
                    self.spvStatusImageView.image = UIImage(named: "installation_detail_panelnumber_spv_verified")
                    self.panelBrandLabel.text = String.localizedStringWithFormat(NSLocalizedString("pvd.detail.panelNumbers.spv.brand", comment: ""), (panelModel.brand))
                    self.panelModelLabel.text = String.localizedStringWithFormat(NSLocalizedString("pvd.detail.panelNumbers.spv.model", comment: ""), (panelModel.model)) 
                default:
                    break
                }
            }else{//没有开启spv
                spvStatusImageViewWidthConstant.update(offset: 0)
                self.panelBrandLabel.text = ""
                self.panelModelLabel.text = ""
            }
            //设置panel number的已上传图片
            self.localFlagImageView.image = UIImage(named: "UploadImageSuccess")
            
            self.panelNumberImageView.image = nil
            if panelModel.image != "" {
                self.panelNumberImageView.backgroundColor = .lightGray
                guard let url = URL(string: panelModel.image) else {
                    return
                }
                self.activityView.startAnimating()
                self.panelNumberImageView.sd_setImage(with: url, placeholderImage: UIImage(named: "installation_detail_panelnumber_list_loadfaild"), options: [SDWebImageOptions.scaleDownLargeImages]) { image, error, cashtype, url in
                    self.activityView.stopAnimating()
                }
            }else{
                self.panelNumberImageView.image = UIImage(named: "installation_detail_panelnumber_list_nopic")
            }

        }else{//operType不为空，表示是本地新增或修改的数据
            //if当前code被编辑过，属于本地数据，不应该显示修改前的spv status
            spvStatusImageViewWidthConstant.update(offset: 0)
            self.panelBrandLabel.text = ""
            self.panelModelLabel.text = ""
            self.localFlagImageView.image = UIImage(named: "uploadImageFailed")
            let imageFilePath = panelModel.image
            if imageFilePath.contains("http"), let url = URL(string: imageFilePath){
                self.activityView.startAnimating()
                self.panelNumberImageView.sd_setImage(with: url, placeholderImage: UIImage(named: "installation_detail_panelnumber_list_loadfaild"), options: [SDWebImageOptions.scaleDownLargeImages]) { image, error, cashtype, url in
                    self.activityView.stopAnimating()
                }
            }else if let image = UIImage(contentsOfFile: imageFilePath){
                self.panelNumberImageView.image = image.generateThumbnailImage()
            }else{
                self.panelNumberImageView.image = UIImage(named: "installation_detail_panelnumber_list_nopic")
            }
        }
    }
}
